<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 获取相关信息文件
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class GetInfo extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		$this->load->model('JayphaTemplate');
		$this->load->dbforge();
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());
		//判断是否登陆
		if(!$this->noLoginErr()) return false;
		$keyword = $this->uri->segment(4);
		switch(strtolower($keyword)){
			case strtolower('delcache'):
				echo $this->cache->file->clean();
				break;
			case strtolower('del'):
				//单项删除
				$tvName = $this->uri->segment(5);
				$id = $this->uri->segment(6);
				$where = array('id'=>$id);
				$this->db->trans_start();
				if(strtolower($tvName)=='channel'){
					$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.$tvName,array('id'=>$id),'');
					if(strlen($subjoinTable)!=0)
						$this->dbforge->drop_table($this->dbTablePrefix.$subjoinTable);
					$this->JayphaDB->delDB($this->dbTablePrefix.'category',array('channelId'=>$id));
					$this->JayphaDB->delDB($this->dbTablePrefix.'archives',array('channelId'=>$id));
				}
				if(strtolower($tvName=='category')){					
					$channelId = $this->JayphaDB->getDBValue('channelId',$this->dbTablePrefix.$tvName,array('id'=>$id),'');
					$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.'channel',array('id'=>$channelId),'');
					$categoryAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id',$this->dbTablePrefix.$tvName,array('parentId'=>$id),'parentId,id',false));
					$categoryIdList[0] = $id;
					if(is_array($categoryAll)){
						if(count($categoryAll)>0){
							for($i=0;$i<count($categoryAll);$i++){
								$categoryIdList[$i+1] = $categoryAll[$i]->id;
							}
						}
					}
					$this->JayphaDB->delDB($this->dbTablePrefix.$subjoinTable,array('db_where_in'=>array('categoryId',$categoryIdList)));
					$this->JayphaDB->delDB($this->dbTablePrefix.'archives',array('db_where_in'=>array('categoryId',$categoryIdList)));
					$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,array('db_where_in'=>array('id',$categoryIdList)));
				}
				if(strtolower($tvName)=='archives'){
					$channelId = $this->JayphaDB->getDBValue('channelId',$this->dbTablePrefix.$tvName,$where,'');
					$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.'channel',array('id'=>$channelId),'');
					$this->JayphaDB->delDB($this->dbTablePrefix.$subjoinTable,array('aid'=>$id));
				}
				$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$where);
				//$this->db->trans_rollback();
				$this->db->trans_complete();
				echo $this->db->trans_status();
				break;
			case strtolower('updateOIndex'):
				//更新排序索引
				$tvName = $this->uri->segment(5);
				if(strlen($tvName)==0) return;
				$id = $this->uri->segment(6);
				$newOIndex = $this->uri->segment(7);
				echo $this->JayphaDB->updateDB(array('oIndex'=>$newOIndex),$this->dbTablePrefix.$tvName,array('id'=>$id));
				break;
			case strtolower('getPermission'):
				//获取管理权限字符串
				echo $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
				break;
			case strtolower('getMainMenu'):
				//获取主菜单
				$this->load->view("admin/$this->adminCurrentTemplate/mainMenu");
				break;
			case strtolower('getMainSubDelMenu'):
				//获取删除子菜单
				$this->load->view("admin/$this->adminCurrentTemplate/mainSubDelMenu");
				break;
			case strtolower('getMainSubMenu'):
				//获取批量操作菜单
				$this->load->view("admin/$this->adminCurrentTemplate/mainSubMenu");
				break;
			case strtolower('check'):
				//判断是否存在
				$tvName = $this->input->get_post('tvName');
				$fieldName = $this->input->get_post('fieldName');
				$fieldValue = $this->input->get_post('fieldValue');
				$id = $this->input->get_post('id');
				$parentIdOrCategoryIdFieldName = $this->input->post('parentIdOrCategoryIdFieldName');
				$parentIdOrCategoryIdFieldValue = $this->input->post('parentIdOrCategoryIdFieldValue');
				//$where = array();
				if(strlen($id)==0){
					if(strlen($parentIdOrCategoryIdFieldValue)==0){
						$where = array('upper('.$fieldName.')'=>strtoupper($fieldValue));
					}else{
						$where = array(
							'upper('.$fieldName.')'=>strtoupper($fieldValue),
						$parentIdOrCategoryIdFieldName=>$parentIdOrCategoryIdFieldValue
						);
					}
				}else{
					if(strlen($parentIdOrCategoryIdFieldValue)==0){
						$where = array(
							'id != '=>$id,
							'upper('.$fieldName.')'=>strtoupper($fieldValue)
						);
					}else{
						$where = array(
							'id != '=>$id,
							'upper('.$fieldName.')'=>strtoupper($fieldValue),
						$parentIdOrCategoryIdFieldName=>$parentIdOrCategoryIdFieldValue
						);
					}
				}
				if(strtolower($tvName)=='category'){
					$channelId = $this->input->get_post('channelId');
					$parentId = $this->input->get_post('parentId');
					$where['channelId'] = $channelId;
					$where['parentId'] = $parentId;
				}
				echo $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
				break;
			case strtolower('checkField'):
				$tvName = 'channel';
				$id = $this->input->post('id');
				$fieldname = strtolower($this->input->post('fieldName'));
				$oldName = strtolower($this->input->post('oldName'));				
				if($fieldname==$oldName){
					echo 0;
					return false;
				}
				$channel = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$tvName,array('id'=>$id),'',false);
				if(count($channel)>0){
					foreach($channel as $channel_item){
						$fieldset = $channel_item->fieldset;
						if(strlen($fieldset)>0){
							echo strlen($this->JayphaTemplate->getSubjoinFieldsByName($fieldname,$fieldset,0));
							return false;
						}
					}
				}
				break;
			case strtolower('categoryChannelChange'):
				$tvName = $this->input->get_post('tvName');
				$channelId = $this->input->get_post('channelId');
				$parentId = $this->input->get_post('parentId');
				$id = $this->input->get_post('id');
				$returnString = "<option value='0' selected='selected'>顶级栏目</option>";
				
				if(strlen($id)>0){
					$categoryId = $this->JayphaDB->getDBValue('parentId',$this->dbTablePrefix.$tvName,array('id'=>$id),'');
				}else{
					$categoryId = 0;
				}
				if(strlen($channelId)>0){
					$condition = array('channelId'=>$channelId);
					if(strlen($id)>0)
						$condition['id !='] = $id;
					$category = $this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.$tvName,$condition,'',false);
					$category = $this->JayphaDB->getChildrenList($category,0,1);
					if(count($category)>0){
						foreach($category as $item){
							$returnString .= '<option value="'.$item->id.'"';
							if($item->id==$categoryId)
								$returnString .= ' selected="selected"';
							$returnString .= '>';
							for($i=0;$i<$item->deep;$i++){
								$returnString .= "&nbsp;|-";
							}
							$returnString .= $item->title.'</option>';
						}
					}
				}
				echo $returnString;
				break;
			case strtolower('categoryParentChange'):
				$tvName = $this->input->get_post('tvName');
				$channelId = $this->input->get_post('channelId');
				$parentId = $this->input->get_post('parentId');
				$id = $this->input->post('id');
				$where = array('parentId'=>$parentId,
								'channelId'=>$channelId);
				if(strlen($id)>0){
					unset($where);
					$where = array('id'=>$id);
					$category = $this->JayphaDB->getDB(0,0,'channelId,parentId,oIndex',$this->dbTablePrefix.$tvName,$where,'',true);
					if(count($category)>0){
						$old_channelId = $category[0]->channelId;
						$old_parentId = $category[0]->parentId;
						$oIndex = $category[0]->oIndex;
						if($old_parentId!=$parentId){
							$oIndex = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('channelId'=>$channelId,'parentId'=>$parentId));
							$oIndex++;
						}
					}
				}else{
					$oIndex = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
					$oIndex++;
				}
				echo $oIndex;
				break;
			case strtolower('archivesCategoryChange'):
				$tvName = $this->input->get_post('tvName');
				$categoryId = $this->input->get_post('categoryId');
				$id = $this->input->get_post('id');
				$where = array('categoryId'=>$categoryId);
				if(strlen($id)>0){
					unset($where);
					$where = array('id'=>$id);
					$oIndex = $this->JayphaDB->getDBValue('oIndex',$this->dbTablePrefix.$tvName,$where,'');
					$old_categoryId = $this->JayphaDB->getDBValue('categoryId',$this->dbTablePrefix.$tvName,$where,'');
					if($old_categoryId!=$categoryId){
						$oIndex = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('categoryId'=>$categoryId));
						$oIndex++;
					}
				}else{
					$oIndex = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
					$oIndex++;
				}
				echo $oIndex;
				break;
		}
	}
	
	function addArchives(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());
		//判断是否登陆
		if(!$this->noLoginErr()) return false;		
		$tvName = $this->input->get_post('tvName');
		$channelId = $this->input->get_post('channelId');
		if(strlen($tvName)>0 and strlen($channelId)>0){
			$adminEditCon = $this->JayphaDB->getDBValue('adminEditCon',$this->dbTablePrefix.'channel',array('id'=>$channelId),'');
			if(strtolower(substr($adminEditCon,0,strlen('http://')))!='http://')
				echo site_url($adminEditCon);
			else
				echo $adminEditCon;
		}
	}
	
	function permission(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());
		//判断是否登陆
		if(!$this->noLoginErr()) return false;
		$tvName = 'admin_group';
		$adminGroupParentId = $this->input->get_post('adminGroupParentId');
		$db_permission = '';
		if(strlen($adminGroupParentId)==0){
			$adminGroupParentId = -1;
		}else{
			$db_permission_where = array('id'=>$adminGroupParentId);
			$db_permission = $this->JayphaDB->getDBValue('permission',$this->dbTablePrefix.$tvName,$db_permission_where,'');
		}
		$string = '';
		//echo $db_permission;
		if(strlen($db_permission)>0 || $adminGroupParentId==0){
			$admin_menu_where = array('disabled'=>0);
			$admin_menu = $this->JayphaDB->getDB(0,0,'id,parentId,identifier,title',$this->dbTablePrefix.'admin_menu',$admin_menu_where,'parentId,oIndex,id',false);
			$admin_menu = $this->JayphaDB->objToArr($admin_menu);
			$admin_menu = $this->JayphaDB->getChildrenList($admin_menu,0,0);
			$string .= "<ul class='permission-ul'>\n";
			$pid = 0 ;
			$per_arr = array('add'=>'添加','edit'=>'修改','del'=>'删除','move'=>'批量','reply'=>'回复','search'=>'搜索');
			foreach($admin_menu as $item){
				$class_str = '';
				if($item->deep==1){
					if(!stristr($db_permission,$item->identifier.':') && $adminGroupParentId!=0){
						continue;
					}
					$class_str = ' class="p_0_'.$item->parentId.'"';
				}
				$string .= "\t<li class=\"permission-$item->deep\"><div id='p_0_$item->id'$class_str>";
				if($item->deep==1) $string .= "<input type='checkbox' name='p_all' id='p_all_$item->id' /><label for='p_all_$item->id'>";
				$string .= $item->title;
				if($item->deep==1) $string .= "</label>";
				$string .= "</div></li>\n";
				if($item->deep==1){
					$string .= "\t<li class=\"permission-".($item->deep+1)."\"><div class='p_all_$item->id'>\n";
					foreach($per_arr as $key=>$val){
						switch(strtolower($item->identifier)){
							case 'systemconfig':
							case 'single':
								if($key=='reply' || $key=='move'){
									$is_write = false;
								}else{
									$is_write = true;
								}
								break;
							case 'channel':
								if($key=='move' || $key=='reply' || $key=='search'){
									$is_write = false;
								}else{
									$is_write = true;
								}
								break;
							case 'admingroup':
							case 'admin':
							case 'adminmenu':														
							case 'category':
							case 'archives':
								if($key=='reply'){
									$is_write = false;
								}else{
									$is_write = true;
								}
								break;
							case 'adminloginlog':
								if($key=='add' || $key=='edit' || $key=='move' || $key=='reply'){
									$is_write = false;
								}else{
									$is_write = true;
								}
								break;
						}
						if($is_write){				
							$string .= "\t\t".'<input type="checkbox" name="permission[]" id="permission_'.$item->id.'_'.$key.'" value="'.$item->identifier.'_'.$key.'" />';
							$string .= '<label for="permission_'.$item->id.'_'.$key.'">'.$val.'</label>'."\n";
						}				
					}
					$string .= "\t</div></li>\n";
				}
				if($pid!=$item->parentId && $item->parentId==3){
					$channel = $this->JayphaDB->getDB(0,0,'id,title,identifier',$this->dbTablePrefix.'channel',array('disabled'=>0),'',true);
					foreach($channel as $channel_item){
						$string .= "\t<li class=\"permission-".($item->deep)."\"><div>";
						if($item->deep==1) $string .= "<input type='checkbox' name='p_all' id='p_all_c_$channel_item->id' /><label for='p_all_c_$channel_item->id'>";
						$string .= $channel_item->title."管理";
						if($item->deep==1) $string .= '</label>';
						$string .= "</div></li>\n";
						if($item->deep==1){
						$string .= "\t<li class=\"permission-".($item->deep+1)."\"><div class='p_all_c_$channel_item->id'>\n";
							foreach($per_arr as $key=>$val){
								switch(strtolower($channel_item->identifier)){
									case 'article':
										if($key=='reply'){
											$is_write = false;
										}else{
											$is_write = true;
										}
										break;
								}
								if($is_write){			
									$string .= "\t\t".'<input type="checkbox" name="permission[]" id="permission_c_'.$channel_item->id.'_'.$key.'" value="'.$channel_item->identifier.'_'.$key.'" />';
									$string .= '<label for="permission_c_'.$channel_item->id.'_'.$key.'">'.$val.'</label>'."\n";
								}				
							}
							$string .= "\t</div></li>\n";
						}
					}
				}
				$pid = $item->parentId;
			}
			$string .= "</ul>\n";
		}else{
			$string .= "<div class='red'>对不起，所在组没有分配置权限！</div>";
		}
		echo $string;
	}
}


/* End of file getInfo.php */
/* Location: ./application/controllers/admin/getInfo.php */